package com.wnnight.wys.mapper.room;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnnight.wys.entity.room.Room;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName: RoomMapper
 * @Description: //TODO
 * @author: CD
 * @date: 2022/8/28-20:14
 * @version: v1.0
 */
@Component
public interface RoomMapper extends BaseMapper<Room> {


    //根据业主id查询房间
    @Select("select * from wys_room where landlord_id=#{landlordId}")
    List<Room> selectByLandlordId(int landlordId);


    @Select("SELECT COUNT(STATUS) FROM wys_room WHERE STATUS = \"正常\" ")
    int selectRoomCountzc();

    @Select("SELECT COUNT(STATUS) FROM wys_room WHERE STATUS = \"出租\" ")
    int selectRoomCountcz();

    @Select("SELECT COUNT(STATUS) FROM wys_room WHERE STATUS = \"空闲\" ")
    int selectRoomCountkx();


    @Select("select * from ${prefix}room where id=#{id}")
    Room selectRoomById1(long id);

    //删除业主
    @Update("update ${prefix}room set landlord_id = null where id=#{id}")
    void deleteLandId(long id);


    //动态修改房间信息
    @UpdateProvider(type = updateRoom.class, method = "update")
    void updateRoom11(Room room);
    class updateRoom {
        public static String update(Room room) {
            String sql = new SQL() {{
                UPDATE("${prefix}room");
                if (room.getBuildingNum() != null) {
                    SET("building_num=#{buildingNum}");
                }
                if (room.getStatus() != null) {
                    SET("status=#{status}");
                }
                if (room.getLandlordId() != null) {
                    SET("landlord_id=#{landlordId}");
                }
                if (room.getPeopleNum() != 0) {
                    SET("people_num=#{peopleNum}");
                }
                WHERE("id=#{id}");
            }}.toString();
            return sql;
        }

    }


    // 查楼栋
    @Select("SELECT\tDISTINCT  building_num,building_id FROM ${prefix}room")
    List<Room>selectBuilding();

    //查询所有
    @Select("select * from ${prefix}room")
    List<Room> selectlistRoom();

    //级联查找
    //添加房屋与人数  （如果是业主的话就调用这个接口  插入业主id）
    @Insert("insert into ${prefix}room(building_num,unit,floor,room_num,people_num,status,landlord_id,building_id,unit_id,floor_id) values(#{buildingNum},#{unit},#{floor},#{roomNum},#{pepleNum},#{status},#{landlordId},#{buildingId},#{unitId},#{floorId})")
    int addRoom(Room room);

    //级联查询 楼号
    @Select("select \tDISTINCT  unit_id,unit from ${prefix}room where building_id=#{buildingId}")
    List<Room> getRoomByBuildingId(Integer buildingId);

    //级联查询 单元
    @Select("select * from ${prefix}room where unit_id=#{unitId}")
    List<Room> getRoomByUnitId(Integer unitId);

    @Select("SELECT DISTINCT `floor`,`floor_id`  FROM  wys_room WHERE unit_id=#{unitId} and building_id=#{buildingId}")
    List<Room> getfoolByunbu(Integer unitId,Integer buildingId);

    @Select("SELECT * FROM  wys_room WHERE unit_id=#{unitId} and building_id=#{buildingId}")
    List<Room> getfoolAllByunbu(Integer unitId,Integer buildingId);

    //级联查询 楼层
    @Select("select * from ${prefix}room where floor_id=#{floorId}")
    List<Room> getRoomByFloorId(Integer floorId);

    //动态查找
    @SelectProvider(type = selectRoom.class,method = "select")
    List<Room> selectRoomBYCondtion(Room room);
    class selectRoom{
        public  String select(final Room room){
            return new SQL(){{
                SELECT("*");
                FROM("${prefix}room");
                if (room.getBuildingNum()!=null){
                    WHERE("building_num=#{buildingNum}");
                }else if (room.getUnit()!=null){
                    WHERE("unit=#{unit}");
                }else if (room.getRoomNum()!=null){
                    WHERE("room_num=#{roomNum}");
                }else if (room.getStatus()!=null){
                    WHERE("status like '%正常%' or status like '%空闲%' or status like '%出租%'");
                }else if (room.getId()!=0){
                    WHERE("id=#{id}");
                }
            }}.toString();
        }
    }

    //业主查房屋信息
    @Select("select * from ${prefix}room where landlord_id=#{landlordId}")
    @Results({
            @Result(column = "id",property = "id",id = true),
            @Result(column = "building_num",property = "buildingNum"),
            @Result(column = "room_num",property = "roomNum"),
            @Result(column = "people_num",property = "peopleNum")
    })
    List<Room> getByLandlordId(int landlordId);


}

